import { IsOptional, IsString, IsNumber, IsEnum, IsBoolean, IsArray } from 'class-validator'
import { Transform, Type } from 'class-transformer'
import { ApiPropertyOptional } from '@nestjs/swagger'

/**
 * 文件查询DTO
 * 用于文件列表查询的筛选和排序参数
 */
export class QueryFileDto {
  @ApiPropertyOptional({
    description: '页码',
    example: 1,
    default: 1,
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  page?: number = 1

  @ApiPropertyOptional({
    description: '每页数量',
    example: 10,
    default: 10,
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  limit?: number = 10

  @ApiPropertyOptional({
    description: '搜索关键词（文件名、描述）',
    example: '文档',
  })
  @IsOptional()
  @IsString()
  search?: string

  @ApiPropertyOptional({
    description: '文件类型分类',
    example: 'document',
    enum: ['image', 'video', 'audio', 'document', 'archive', 'other'],
  })
  @IsOptional()
  @IsEnum(['image', 'video', 'audio', 'document', 'archive', 'other'])
  category?: string

  @ApiPropertyOptional({
    description: 'MIME类型',
    example: 'application/pdf',
  })
  @IsOptional()
  @IsString()
  mimeType?: string

  @ApiPropertyOptional({
    description: '文件状态',
    example: 'completed',
    enum: ['pending', 'uploaded', 'processing', 'completed', 'error'],
  })
  @IsOptional()
  @IsEnum(['pending', 'uploaded', 'processing', 'completed', 'error'])
  status?: string

  @ApiPropertyOptional({
    description: '是否公开',
    example: true,
  })
  @IsOptional()
  @Transform(({ value }) => value === 'true')
  @IsBoolean()
  isPublic?: boolean

  @ApiPropertyOptional({
    description: '文件标签',
    example: ['重要', '项目A'],
    type: [String],
  })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  tags?: string[]

  @ApiPropertyOptional({
    description: '上传者ID',
    example: 1,
  })
  @IsOptional()
  @IsNumber()
  uploaderId?: number

  @ApiPropertyOptional({
    description: '最小文件大小（字节）',
    example: 1024,
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  minSize?: number

  @ApiPropertyOptional({
    description: '最大文件大小（字节）',
    example: 10485760,
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  maxSize?: number

  @ApiPropertyOptional({
    description: '开始日期（ISO格式）',
    example: '2024-01-01T00:00:00.000Z',
  })
  @IsOptional()
  @IsString()
  startDate?: string

  @ApiPropertyOptional({
    description: '结束日期（ISO格式）',
    example: '2024-12-31T23:59:59.999Z',
  })
  @IsOptional()
  @IsString()
  endDate?: string

  @ApiPropertyOptional({
    description: '排序字段',
    example: 'createdAt',
    enum: ['createdAt', 'updatedAt', 'size', 'downloadCount', 'originalName'],
  })
  @IsOptional()
  @IsEnum(['createdAt', 'updatedAt', 'size', 'downloadCount', 'originalName'])
  sortBy?: string = 'createdAt'

  @ApiPropertyOptional({
    description: '排序方向',
    example: 'DESC',
    enum: ['ASC', 'DESC'],
    default: 'DESC',
  })
  @IsOptional()
  @IsEnum(['ASC', 'DESC'])
  sortOrder?: 'ASC' | 'DESC' = 'DESC'
}

/**
 * 文件更新DTO
 */
export class UpdateFileDto {
  @ApiPropertyOptional({
    description: '文件描述',
    example: '更新后的文件描述',
  })
  @IsOptional()
  @IsString()
  description?: string

  @ApiPropertyOptional({
    description: '文件标签',
    example: ['文档', '重要', '项目A'],
    type: [String],
  })
  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  tags?: string[]

  @ApiPropertyOptional({
    description: '是否公开访问',
    example: true,
  })
  @IsOptional()
  @IsBoolean()
  isPublic?: boolean

  @ApiPropertyOptional({
    description: '可读权限用户ID列表',
    example: [1, 2],
    type: [Number],
  })
  @IsOptional()
  @IsArray()
  @IsNumber()
  readPermissions?: number[]

  @ApiPropertyOptional({
    description: '可写权限用户ID列表',
    example: [1, 2],
    type: [Number],
  })
  @IsOptional()
  @IsArray()
  @IsNumber()
  writePermissions?: number[]

  @ApiPropertyOptional({
    description: '可删除权限用户ID列表',
    example: [1, 2],
    type: [Number],
  })
  @IsOptional()
  @IsArray()
  @IsNumber()
  deletePermissions?: number[]
}
